package cn.klandor.picasa.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public class GPicasa {
	
	private static int photoWidth = 40;
	private static int photoHeight = 40;
	private static int photoSpacing = 2;
	private static BufferedImage gradientImage = createGradientMask(photoWidth,photoHeight);
	private static BufferedImage myImageFrame = getMyImageFrame();
	
	public GPicasa(){
		
	}
	
	private static BufferedImage getMyImageFrame() {
		BufferedImage myBuffer = new BufferedImage(photoWidth, photoHeight,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = myBuffer.createGraphics();
       
        g2.setPaint(Color.WHITE);
        Area area = new Area(new Rectangle(0, 0, photoWidth, photoHeight));
        g2.fill(area);
		g2.dispose();
		return myBuffer;
	}

	public static BufferedImage TranslateToFortyIcon(BufferedImage image){
		
		double Ratio_x = (new Integer(photoWidth)).doubleValue() / image.getWidth();
		double Ratio_y = (new Integer(photoHeight)).doubleValue() / image.getHeight();
        
        AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio_x, Ratio_y), null);
        image = op.filter(image, null);
        myImageFrame = getMyImageFrame();
        myImageFrame.createGraphics().drawImage(image,photoSpacing,photoSpacing,(photoWidth-photoSpacing*2),(photoHeight-photoSpacing*2),null); 
        
		return createReflectedPicture(myImageFrame);
	}

	private static BufferedImage createReflectedPicture(BufferedImage image) {
		BufferedImage alphaMask = gradientImage;
		BufferedImage bufferImg = createReflection(image,photoWidth, photoHeight);

		return applyAlphaMask(bufferImg, alphaMask, photoWidth, photoHeight);
	}

	private static BufferedImage applyAlphaMask(BufferedImage bufferImg,
											BufferedImage alphaMask,
											int photoWidth, int photoHeight) {
		Graphics2D g2 = bufferImg.createGraphics();
        g2.setComposite(AlphaComposite.DstOut);
        g2.drawImage(alphaMask, null, 0, photoHeight);
        g2.dispose();
        
		return bufferImg;
	}

	private static BufferedImage createReflection(BufferedImage image,int photoWidth, int photoHeight) {
		BufferedImage buffer = new BufferedImage(photoWidth, photoHeight << 1,BufferedImage.TYPE_INT_ARGB);

		Graphics2D g = buffer.createGraphics();
		g.drawImage(image, null, null);
		g.translate(0, photoHeight << 1);

		AffineTransform reflectTransform = AffineTransform.getScaleInstance(1.0, -1.0);
		g.drawImage(image, reflectTransform, null);
		g.translate(0, -(photoHeight << 1));

		g.dispose();

		return buffer;
	}

	private static BufferedImage createGradientMask(int photoWidth, int photoHeight) {
		BufferedImage gradient = new BufferedImage(photoWidth, photoHeight,BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = gradient.createGraphics();
		GradientPaint painter = new GradientPaint(0.0f, 0.0f,
									new Color(1.0f, 1.0f, 1.0f, 0.5f),
									0.0f, photoHeight / 2.0f,
									new Color(1.0f, 1.0f, 1.0f, 1.0f));
		g.setPaint(painter);
		g.fill(new Rectangle2D.Double(0, 0, photoWidth, photoHeight));

		g.dispose();

		return gradient;

	}

}
